package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: lihan
 * @Date : 2021/12/14- 12
 * @DESCRIPTION: com.atguigu.springcloud.controller
 * @version: 1.0
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "global_paymentInfoTimeoutHandler")
public class OrderHystrixController {
    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String result  = paymentHystrixService.paymentInfo_OK(id);
        log.info("*************result:"+result);
        return  result;

    }

    @HystrixCommand(fallbackMethod = "paymentInfoTimeoutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        log.info("*************result:"+result);
        return result;
    }

    public String paymentInfoTimeoutHandler(Integer id){
        return "我是消费者80："+"  paymentInfoTimeoutHandler: "+id+"\t";
    }

    public String global_paymentInfoTimeoutHandler(Integer id){
        return "我是全局："+"  paymentInfoTimeoutHandler: "+id+"\t";
    }

}
